package com.learn.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 判断一个树是否为完全二叉树
 * <p>
 * 条件1：当遇到右孩子不为空，左孩子为空，则为False
 * 条件2：当遇到有一个节点有不双全的孩子，则代表后续的节点都是叶子节点
 */
public class BSTree {
    public static class Node {
        private Node left;
        private Node right;
        private int value;

        public Node(int value) {
            this.value = value;
        }
    }

    public boolean isBSTree(Node node) {
        if (node == null) {
            return true;
        }
        Queue<Node> queue = new LinkedList<>();
        //是否遇到叶子节点
        boolean isLeaf = false;
        queue.add(node);
        Node left = null;
        Node right = null;
        while (!queue.isEmpty()) {
            Node node1 = queue.poll();
            left = node1.left;
            right = node1.right;
            if (//条件1  左孩子为空，右孩子不为空
                    (left == null && right != null)
                            ||
                            //条件2 已经是叶子节点，但是存在左孩子或者右孩子
                            (isLeaf && (left != null || right != null))) {
                return false;
            }

            if (left != null) {
                queue.add(left);
            }
            if (right != null) {
                queue.add(right);
            }
            //如果开始出现一个孩子为空的情况，则认定接下来的节点都将是叶子节点
            if (left == null || right == null) {
                isLeaf = true;
            }
        }
        return true;
    }


    //递归解法

    public static class Info {
        //是否为满二叉树
        private boolean isFullTree;
        //是否为完全二叉树
        private boolean isCBTree;
        private int height;

        public Info(boolean isFullTree, boolean isCBTree, int height) {
            this.isCBTree = isCBTree;
            this.isFullTree = isFullTree;
            this.height = height;
        }
    }


    public boolean isCBTree2(Node head) {
        return process(head).isCBTree;
    }

    public Info process(Node head) {
        if (head == null) {
            return new Info(true, true, 0);
        }

        Info leftInfo = process(head.left);
        Info rightInfo = process(head.right);

        //左边为满二叉树且右边为满二叉树，且高度相等
        boolean isFullTree = leftInfo.isFullTree && rightInfo.isFullTree && leftInfo.height == rightInfo.height;

        int height = Math.max(leftInfo.height, rightInfo.height) + 1;
        boolean isCBTree = false;

        if (isFullTree) {
            isCBTree = true;
        } else {
            //以当前节点为整棵树，不满的情况判断
            if (leftInfo.isCBTree && rightInfo.isCBTree) {
                //左边为完全二叉树，右边为满二叉树，左右高度差为1，则为完全
                if (rightInfo.isFullTree && leftInfo.isCBTree && leftInfo.height - rightInfo.height == 1) {
                    isCBTree = true;
                }
                //左为满，右为完全，左右高度相等 ，则为完全
                if (leftInfo.isFullTree && rightInfo.isCBTree && leftInfo.height == rightInfo.height) {
                    isCBTree = true;
                }
                //左为满，右为满，左右高度相等
                if (leftInfo.isFullTree && rightInfo.isFullTree && leftInfo.height == rightInfo.height+1) {
                    isCBTree = true;
                }
            }

        }

        return new Info(isFullTree, isCBTree, height);
    }
}
